﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DotNetStar.TheKing.Web.Pages
{
    using Services;
    using Domain;

    public class MySignInModel : PageModel
    {
        private readonly UserService _userService;
        public MySignInModel(UserService userService)
        {
            _userService = userService;
        }
        

        /// <summary>
        /// 获取或设置当月的日历。
        /// </summary>
        public IEnumerable<Tuple<DateTime,bool>> Calendar { get; set; }

        public int Year { get; set; }
        public int Month { get; set; }


        public async Task<IActionResult> OnGetAsync(int? year= default(int?), int? month=default(int?))
        {
            year = year ?? DateTime.Now.Year;
            month = month ?? DateTime.Now.Month;

            var days = DateTime.DaysInMonth(year.Value, month.Value);

            var signInList = await _userService.GetUserSignInList(User.Identity.UserId(), year.Value, month.Value);
            this.Calendar = new HashSet<Tuple<DateTime,bool>>();

            for (int i = 1; i <= days; i++)
            {
                var currentDate = new DateTime(year.Value, month.Value, i);
                (this.Calendar as ICollection<Tuple<DateTime, bool>>).Add(new Tuple<DateTime, bool>(currentDate, signInList.Any(m => m.SignDate == currentDate)));
            }

            this.Year = year.Value;
            this.Month = month.Value;

            return Page();
        }
    }
}